<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="16" >
            <a-form-item label="发票号码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['fphm']" placeholder="请输入发票号码"></a-input>
            </a-form-item>
          </a-col>
          <!--<a-col :span="8" >-->
          <!--  <a-form-item label="发票代码" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
          <!--    <a-input v-decorator="['fpdm']" placeholder="请输入发票代码"></a-input>-->
          <!--  </a-form-item>-->
          <!--</a-col>-->
          <a-col :span="8" >
            <a-form-item label="发票样式" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <!--<a-input v-decorator="['fpys']" placeholder="请选择发票样式"></a-input>-->
              <j-dict-select-tag v-decorator="['fpys', { rules: [{ required: true, message: '请选择发票样式!' }] }]" :triggerChange="true" dictCode="fpys_dict" placeholder="请选择发票样式"></j-dict-select-tag>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24">
            <p>
              <a-divider orientation="left">• 购方信息</a-divider>
            </p>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12" >
            <a-form-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['gfmc']" placeholder="请输入购方名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="开户行" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['gfkhh']" placeholder="请输入购方开户行"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12" >
            <a-form-item label="账号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['gfzh']" placeholder="请输入购方账号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="纳税人识别号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['gfnsrsbh']" placeholder="请输入购方纳税人识别号"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12" >
            <a-form-item label="地址、电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['gfdzdh']" placeholder="请输入购方地址、电话"></a-input>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="24">
            <p>
              <a-divider orientation="left">• 开票方</a-divider>
            </p>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12" >
            <a-form-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['xfmc']" placeholder="请输入销方名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="开户行" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['xfkhh']" placeholder="请输入销方开户行"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12" >
            <a-form-item label="账号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['xfzh']" placeholder="请输入销方账号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="纳税人识别号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['xfnsrsbh']" placeholder="请输入销方纳税人识别号"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12" >
            <a-form-item label="地址、电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['xfdzdh']" placeholder="请输入销方地址、电话"></a-input>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="24">
            <p>
              <a-divider orientation="left">• 发票内容</a-divider>
            </p>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12" >
            <a-form-item label="货物或劳物服务名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['fpnr']" placeholder="请输入发票内容"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="开票总金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['kpzje']" placeholder="请输入开票总金额"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12" >
            <a-form-item label="税率" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['sl']" placeholder="请输入税率"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="税额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['se']" placeholder="请输入税额"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12" >
            <a-form-item label="邮寄地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['yjdz']" placeholder="请输入邮寄地址"></a-input>
            </a-form-item>
          </a-col>
          <a-row>
            <a-col :span="24">
              <p>
                <a-divider orientation="left"></a-divider>
              </p>
            </a-col>
          </a-row>
        </a-row>
        <a-row>
          <a-col :span="12" >
            <a-form-item label="开票人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['createBy']" placeholder="请输入开票人"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="操作时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['createTime']" placeholder="请输入开票人"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24">
            <p>
              <a-divider orientation="left"></a-divider>
            </p>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12" >
            <a-form-item label="发票文件" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <!--<a-input v-decorator="['fptp']" placeholder="请上传发票文件"></a-input>-->
              <!--<j-image-upload isMultiple number="1" v-decorator="['fptp']" placeholder="请上传发票图片"></j-image-upload>-->
              <j-upload v-decorator="['fptp', validatorRules.fptp]" :trigger-change="true"></j-upload>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="发票项目明细" :key="refKeys[0]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[0]"
          :loading="lhygInvoiceProTable.loading"
          :columns="lhygInvoiceProTable.columns"
          :dataSource="lhygInvoiceProTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="false"
          :actionButton="false"/>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import pick from 'lodash.pick'
  import { getAction } from '@/api/manage'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JImageUpload from '@/components/jeecg/JImageUpload'
  import JUpload from '@/components/jeecg/JUpload'

  export default {
    name: 'LhygInvoiceForm',
    mixins: [JEditableTableMixin],
    components: {
      JFormContainer,
      JImageUpload,
      JUpload,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
        },
        refKeys: ['lhygInvoicePro', ],
        tableKeys:['lhygInvoicePro', ],
        activeKey: 'lhygInvoicePro',
        // 发票项目明细表
        lhygInvoiceProTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '项目名称',
              key: 'xmmc',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '项目类型',
              key: 'xmlx',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '项目编号',
              key: 'xmbh',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '发布时间',
              key: 'fbsj',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '累计支付金额',
              key: 'ljzfje',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
          ]
        },
        url: {
          add: "/invoice/lhygInvoice/add",
          edit: "/invoice/lhygInvoice/edit",
          queryById: "/invoice/lhygInvoice/queryById",
          lhygInvoicePro: {
            list: '/invoice/lhygInvoice/queryLhygInvoiceProByMainId'
          },
        }
      }
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：false流程表单 true普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      addBefore(){
        this.form.resetFields()
        this.lhygInvoiceProTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'fphm','fpdm','fpys','gfmc','gfkhh','gfzh','gfnsrsbh','gfdzdh','xfmc','xfkhh','xfzh','xfnsrsbh','xfdzdh','fpnr','kpzje','sl','se','yjdz','kpr','fptp','createBy','createTime')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.lhygInvoicePro.list, params, this.lhygInvoiceProTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          lhygInvoiceProList: allValues.tablesValue[0].values,
        }
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          })
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'fphm','fpdm','fpys','gfmc','gfkhh','gfzh','gfnsrsbh','gfdzdh','xfmc','xfkhh','xfzh','xfnsrsbh','xfdzdh','fpnr','kpzje','sl','se','yjdz','kpr','fptp','createBy','createTime'))
     },

    }
  }
</script>

<style scoped>
</style>